package org.javaforever.cookieshop.dao;

import java.util.Set;
import org.javaforever.cookieshop.domain.PermissionBean;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import jakarta.transaction.Transactional;
public interface PermissionDao extends JpaRepository<PermissionBean, Long>{
	@Query("from PermissionBean p")
	public List<PermissionBean> listAllPermissions() throws Exception;

	@Modifying
	@Query("delete from PermissionBean p where p.id = :id")
	@Transactional
	public void deletePermission(Long id) throws Exception;

	@Modifying
	@Query("update PermissionBean p set p.active = false where p.id = :id")
	@Transactional
	public void softDeletePermission(Long id) throws Exception;

	@Query("from PermissionBean p where p.id = :id")
	public PermissionBean findPermissionById(Long id) throws Exception;

	@Query("from PermissionBean p where p.permName = :permName")
	public PermissionBean findPermissionByPermName(String permName) throws Exception;

	@Query("from PermissionBean p where p.active = true")
	public List<PermissionBean> listActivePermissions() throws Exception;

	@Modifying
	@Query("update PermissionBean p set p.active = not p.active where p.id = :id")
	@Transactional
	public void togglePermission(Long id) throws Exception;

	@Modifying
	@Query("update PermissionBean p set p.active = true where p.id = :id")
	@Transactional
	public void activatePermission(Long id) throws Exception;

	@Query("from PermissionBean p where p.id = :id")
	public PermissionBean findPermissionFullInfoById(Long id) throws Exception;

	@Query("from PermissionBean p where p.permName like CONCAT(:shortName, '%')")
	public List<PermissionBean> searchPermissionIndexedNamesByShortName(String shortName) throws Exception;

	@Query("select count(*) as countNum from PermissionBean p where p.active = true")
	public Integer countActivePermissionRecords() throws Exception;

	@Query(value="select distinct cs_permissions.id,cs_permissions.perm_name,cs_permissions.active from cs_permissions,cs_role_permission where cs_permissions.id = cs_role_permission.permission_id and cs_role_permission.role_id = :roleId and cs_permissions.active = true order by cs_permissions.perm_name asc ", nativeQuery=true)
	public Set<PermissionBean> listActiveRolePermissionsUsingRoleId(Long roleId) throws Exception;

}
